package threadcoreknowledge;

/**
 * @Author lhj
 * @Date 2023-07-20 20:35
 */
public class TestA implements Runnable {
    private static Object lock = new Object();
    private volatile static int count;

    @Override
    public void run() {
        while (count < 100) {
            synchronized (lock) {
                System.out.println("线程" + Thread.currentThread().getName() + ":" + count++);
                lock.notify();
                try {

                    // 加判断是为了最后一次不等待,不然程序没法终止
                    if (count < 99) {
                        lock.wait();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new Thread(new TestA(), "偶数").start();
        new Thread(new TestA(), "奇数").start();
    }
}
